package com.lc.luzhengtang.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.luzhengtang.R;
import com.lc.luzhengtang.bean.NewsBean;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.List;

/**
 * Created by AYD on 2016/11/21.
 * <p/>
 * 购物车Adapter
 */
public class NewsListTestAdapter extends BaseAdapter {

    private boolean isShow = true;//是否显示编辑/完成
    private List<NewsBean.DataBeanX.DataBean> shoppingCartBeanList;
    private CheckInterface checkInterface;
    private ModifyCountInterface modifyCountInterface;
    private Context context;
    private int TYPE_ITEM = 0;
    public NewsListTestAdapter(Context context,int TYPE_ITEM, List<NewsBean.DataBeanX.DataBean> shoppingCartBeanList) {
        this.context = context;
        this.TYPE_ITEM=TYPE_ITEM;
        this.shoppingCartBeanList = shoppingCartBeanList;
    }

    public void setShoppingCartBeanList(Context context, List<NewsBean.DataBeanX.DataBean> shoppingCartBeanList) {
        this.shoppingCartBeanList = shoppingCartBeanList;
        this.context = context;
        notifyDataSetChanged();

    }

    /**
     * 单选接口
     *
     * @param checkInterface
     */
    public void setCheckInterface(CheckInterface checkInterface) {
        this.checkInterface = checkInterface;
    }

    /**
     * 改变商品数量接口
     *
     * @param modifyCountInterface
     */
    public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {
        this.modifyCountInterface = modifyCountInterface;
    }

    @Override
    public int getCount() {
        return shoppingCartBeanList == null ? 0 : shoppingCartBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return shoppingCartBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    /**
     * 是否显示可编辑
     *
     * @param flag
     */
    public void isShow(boolean flag) {
        isShow = flag;
        notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
//            convertView = LayoutInflater.from(context).inflate(R.layout.item_list_test, parent, false);
            convertView = BoundViewHelper.boundView(this, ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) LayoutInflater.from(context)
                    .inflate(R.layout.item_edit_news, null)));
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (TYPE_ITEM == 0) {

            holder.checkbox_news.setVisibility(View.GONE);
            holder.layout_flag.setVisibility(View.VISIBLE);


        } else {
            holder.checkbox_news.setVisibility(View.VISIBLE);
            holder.layout_flag.setVisibility(View.GONE);

        }
        final NewsBean.DataBeanX.DataBean shoppingCartBean = shoppingCartBeanList.get(position);

        if (shoppingCartBean.getState() == 1) {
            holder.text_flag.setText("未读");
            holder.text_flag.setTextColor(context.getResources().getColor(R.color.red));
            holder.view_back_news.setBackgroundResource(R.drawable.background_circle);
        } else {
            holder.text_flag.setText("已读");
            holder.text_flag.setTextColor(context.getResources().getColor(R.color.gray2));
            holder.view_back_news.setBackgroundResource(R.drawable.background_circle_gray);
        }

        holder.text_newstitle.setText(shoppingCartBean.getTitle());
        holder.text_newstime.setText(shoppingCartBean.getCreate_time());
        holder.checkbox_news.setChecked(shoppingCartBean.isChoosed());
        //单选框按钮
        holder.checkbox_news.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        shoppingCartBean.setChoosed(((CheckBox) v).isChecked());
                        checkInterface.checkGroup(position, ((CheckBox) v).isChecked());//向外暴露接口
                    }
                }
        );


        return convertView;
    }


    //初始化控件
    class ViewHolder {

        CheckBox checkbox_news;

        TextView text_newstitle;
        LinearLayout layout_flag;
        //标记已读
        TextView text_flag;
        //view
        View view_back_news;
        TextView text_newstime;
        public ViewHolder(View itemView) {
            checkbox_news = (CheckBox) itemView.findViewById(R.id.checkbox_news);
            text_newstitle = (TextView) itemView.findViewById(R.id.text_newstitle);
            layout_flag = (LinearLayout) itemView.findViewById(R.id.layout_flag);
            view_back_news = itemView.findViewById(R.id.view_back_news);
            //标记
            text_flag = (TextView) itemView.findViewById(R.id.text_flag);
            text_newstime= (TextView) itemView.findViewById(R.id.text_newstime);
        }

    }


    /**
     * 复选框接口
     */
    public interface CheckInterface {
        /**
         * 组选框状态改变触发的事件
         *
         * @param position  元素位置
         * @param isChecked 元素选中与否
         */
        void checkGroup(int position, boolean isChecked);
    }


    /**
     * 改变数量的接口
     */
    public interface ModifyCountInterface {
        /**
         * 增加操作
         *
         * @param position      组元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param isChecked     子元素选中与否
         */
        void doIncrease(int position, View showCountView, boolean isChecked);

        /**
         * 删减操作
         *
         * @param position      组元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param isChecked     子元素选中与否
         */
        void doDecrease(int position, View showCountView, boolean isChecked);

        /**
         * 删除子item
         *
         * @param position
         */
        void childDelete(int position);
    }
}
